package weavingdraft;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

/**
 * 
 * @author Justin Wong
 * 
 * This class creates the squares to be used in the PedalGrid class. 
 *
 */
@SuppressWarnings("serial")
public class PedalSquare extends JPanel{
	/**
	 * The WeavingDraft to be working on. 
	 */
	private WeavingDraft my_draft;
	/**
	 * The object's row.
	 */
	private int my_row;
	/**
	 * The object's column.
	 */
	private int my_column;
	/**
	 * The PedalGrid object it refers to. 
	 */
	private PedalGrid my_pedal_panel;
	/**
	 * Checks for double clicks. 
	 */
	private boolean double_click;
	/**
	 * number to help determine if this should be black or white.
	 */
	int number;
	/**
	 * Creates the PedalSquare object storing the information in this class. 
	 * @param the_row the row this object refers to. 
	 * @param the_column the column this object refers to. 
	 * @param the_draft the Weaving draft this object refers to. 
	 * @param the_grid the PedalGrid this draft works from. 
	 */
	public PedalSquare(int the_row, int the_column, WeavingDraft the_draft, PedalGrid the_grid){
		my_draft = the_draft;
		my_row = the_row;
		my_column = the_column;
		my_pedal_panel = the_grid;
		double_click = false;
		number = 0;
		this.addMouseListener(new MouseAction(this));
		
	}
	/**
	 * Updates and paints the square. 
	 */
	@Override
	public void paintComponent(Graphics the_graphics){
		Graphics2D variable_graphics = (Graphics2D) the_graphics;
		int width = this.getWidth();
		int height = this.getHeight();

		if(number % 2 == 0){
			if(double_click){
				my_pedal_panel.setSquare(null, my_row);
				double_click = false;
				my_draft.deletePedal(my_row, my_column);
			}
			variable_graphics.setColor(Color.WHITE);
			variable_graphics.fillRect(0, 0, width - 1, height - 1);
			variable_graphics.setColor(Color.BLACK);
			variable_graphics.drawRect(0, 0, width - 1, height - 1);
		}else{
			if(my_pedal_panel.getSquare(my_row) != null){
				PedalSquare s = my_pedal_panel.getSquare(my_row);
				s.addCount();
				s.repaint();
			}
			my_draft.setPedallingGrid(my_row, my_column);
			my_pedal_panel.setSquare(this, my_row);
			variable_graphics.setColor(Color.BLACK);
			variable_graphics.fillRect(0, 0, width - 1, height - 1);
			variable_graphics.setColor(Color.WHITE);
			variable_graphics.drawRect(0, 0, width - 1, height - 1);
		}
	}
	/**
	 * Determines if the object is black or white and helps repaint the object. 
	 */
	public void addCount(){
		number++;
		repaint();
	}
	/**
	 * Returns the column number. 
	 * @return the column number. 
	 */
	public int getColumn(){
		return my_column;
	}
	/**
	 * Creates the action of the class by allowing this to be clicked. 
	 * @author Justin Wong
	 *
	 */
	class MouseAction extends MouseInputAdapter{
		/**
		 * The panel this object refers to. 
		 */
		private JPanel my_panel;
		/**
		 * Attaches the action to the panel. 
		 * @param the_panel the panel being referred to. 
		 */
		private MouseAction(JPanel the_panel){
			my_panel = the_panel;
		}
		/**
		 * The action of what happens when the mouse is clicked. 
		 */
		@Override
		public void mouseClicked(MouseEvent e){
			number++;
			PedalSquare s = my_pedal_panel.getSquare(my_row);
			if(s != null && s.getColumn() == my_column){
				double_click = true;
			}
			my_panel.repaint();
		}
	}

}
